<?php

namespace app\controller;

use app\BaseController;
use app\common\model\CircleConfig;
use app\common\model\Device;
use app\common\model\Message;
use app\common\model\Product;
use app\common\model\Testimonials;
use app\model\Banner;
use think\facade\View;
use Throwable;

class Index extends BaseController
{
    public function index()
    {
        //轮播图
        $res = Banner::where(['status' => 1])->select()->toArray();
        View::assign('banner', $res);
        //首页案例显示
        $case = Product::where(['status' => 1, 'is_home' => 1])->limit(6)->order('create_time', 'desc')->select()->toArray();
        View::assign('case', $case);
        //首页我的设备
        $device = Device::where(['status' => 1, 'is_home' => 1])->limit(4)->order('create_time', 'desc')->select()->toArray();
        View::assign('device', $device);
        //客户说
        $testimonials = Testimonials::where(['status' => 1])->order('create_time', 'desc')->select()->toArray();
        $new_arr = [];

        foreach ($testimonials as $key => $value) {
            $new_arr['testimonial_slider'][$key]['desc'] = $value['desc'];
            $new_arr['testimonial_thumbnil'][$key]['title'] = $value['title'];
            $new_arr['testimonial_thumbnil'][$key]['url'] = $value['url'];
        }
        View::assign('testimonial_slider', $new_arr['testimonial_slider']);
        View::assign('testimonial_thumbnil', $new_arr['testimonial_thumbnil']);

        //首页配置
        $configImg = get_sys_config('', 'home');
        $reset_str_home_title = explode(',', $configImg['home_title']);
        View::assign('home_title', reset($reset_str_home_title));
        View::assign('end_title', end($reset_str_home_title));
        View::assign('home_title_en', $configImg['home_title_en']);
        View::assign('home_img', $configImg['home_img']);
        View::assign('home_bg_up_img', $configImg['home_bg_up_img']);
        View::assign('testimonials_img', $configImg['testimonials_img']);
        View::assign('case_img', $configImg['case_img']);
        View::assign('device_img', $configImg['device_img']);
        View::assign('home_contact_img', $configImg['home_contact_img']);

        //占比
        $circle_config = CircleConfig::where(['status' => 1])->limit(3)->order('create_time', 'desc')->select()->toArray();
        foreach ($circle_config as $key => $value) {
            $circle_config[$key]['ratio_value'] = ($value['ratio'] / 100);
        }
        View::assign('circle_config', $circle_config);


        return View::fetch();
    }


    //保存消息提交
    public function contactProcess()
    {
        if ($this->request->isPost()) {
            $model = new Message();
            $data = $this->request->post();

            if (!$data) {
                $this->error(('参数不能为空'));
            }
            $result = false;
            $model->startTrans();
            try {
                $result = $model->save($data);
                $model->commit();
            } catch (Throwable $e) {
                $model->rollback();
                $this->error($e->getMessage());
            }
            if ($result !== false) {
                $this->success('提交成功');
            } else {
                $this->error(('提交失败'));
            }
        }
        $this->error(('参数错误'));
    }
}
